package moldat;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Diese Klasse verarbeitet alle Anfragen, die von der 'OverviewPage' gestellt
 * werden. Ueber dieses Servlet koennen alle vorhandenen Informationen zu einer
 * UniProtID angezeigt werden.
 *
 * @author Marc Zoeller
 */
public class OverviewServlet extends HttpServlet {

    /**
     * Verarbeitet alle Post- und Get-Requests an dieses Servlet.
     *
     * @param request HTML-Request des Clients
     * @param response HTML-Response des Servers
     * @throws ServletException
     * @throws IOException
     */
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        ServletContext context = getServletContext();
        request.setAttribute("title", "Overview");
        if (request.getParameter("protein") != null) {
            String input = request.getParameter("protein");
            try {
                Protein p = SQLiteDB.getProtein(input);
                //TODO: neue Informationen auch anzeigen
                if (p != null) {
                    request.setAttribute("id", p.getUniProtID());
                    request.setAttribute("name", p.getName());
                    request.setAttribute("gene", p.getGene());
                    request.setAttribute("synonyms", p.getGeneSynonyms());
                    request.setAttribute("isoforms", p.getIsoforms());
                    request.setAttribute("arrayExpress", p.getArrayExpressUrl());
                }
                else
                    request.setAttribute("error", "No entry found.");
            } catch (SQLException ex) {
                System.out.println("Error while loading protein " + input + ": " + ex.getMessage());
                request.setAttribute("error", ex.getMessage());
            }
        }
        context.getNamedDispatcher("OverviewPage").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}